package cn.com.haopy.yue.ai.wx.web;


import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.core.utils.SseClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.UUID;

/**
 * @Author: best_liu
 * @Description:
 * @Date Create in 13:20 2024/6/17
 * @Modified By:
 */
@RestController
@RequestMapping("/haopyYueAi/sse")
@Api(tags = "SSE连接")
@Slf4j
public class WxSseController {
    @Autowired
    private SseClient sseClient;

    @CrossOrigin
    @GetMapping("/connect")
    @ApiOperation("创建连接")
    public SseEmitter createConnect(String uid) {
        return sseClient.createSse(uid);
    }

    /**
     * 关闭连接
     */
    @CrossOrigin
    @GetMapping("/closeConnect")
    @ApiOperation("关闭")
    public Object closeConnect(String uid) {
        try {
            sseClient.closeSse(uid);
        } catch (Exception e) {
            log.error(e.toString());
        }

        return ResponseUtil.ok();

    }


    @CrossOrigin
    @GetMapping("/sendMsg")
    @ResponseBody
    @ApiOperation("发消息")
    public Object sseChat(String uid) {

        Boolean bool = sseClient.sendMessage(uid, UUID.randomUUID().toString(), "测试订单提醒消息");
        if (bool) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.failMsg("发消息失败！");
    }

    @CrossOrigin
    @GetMapping("/allClient")
    @ResponseBody
    @ApiOperation("全部sse连接")
    public Object allClient(String uid) {
        return ResponseUtil.ok(sseClient.getAllSseEmitter());
    }


}
